package com.info.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.info.data.GlobalData;

public class DownLoadFile extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DownLoadFile() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


    
    public void downLoad(HttpServletResponse response, String headName, String fileUrl) throws Exception{
        
//        Logger.log("begin download " + headName);        
        if (headName.endsWith(".jpg") || (headName.endsWith(".gif"))) {
            //no need setting content type and header
        } else if (headName.endsWith(".txt")) {
            response.setContentType( "text;charset=GB2312");
            response.setHeader("Content-disposition",  
                              "attachment; filename=" + headName );
        } else {
            response.setContentType( "application/doc;charset=GB2312");
            response.setHeader("Content-disposition",  
                              "attachment; filename=" + headName );
        }

        String fileURL = fileUrl;
//        Logger.log(fileURL);
        if(fileURL == null)
            return;
        
        try{
            File file = new File(fileUrl);            
            FileInputStream bis = new FileInputStream(file);            
            OutputStream bos = response.getOutputStream(); 

            byte[] buff = new byte[1024];
            int readCount = 0;
            int i = 0;
            readCount = bis.read(buff);
            while (readCount != -1){
               bos.write(buff, 0, readCount); 
               readCount = bis.read(buff);
            }
//            Logger.log("read finished!");
            if (bis!=null)
                bis.close();            
            if (bos!=null)
                bos.close();

        }catch(Exception e){
           e.printStackTrace();           
           throw e;
        }
    }


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		
		String fileName= request.getParameter("fileName");
		
		String path=GlobalData.upFilePath+File.separator+fileName;
		try {
			downLoad(response, fileName,path);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		PrintWriter out = response.getWriter();
//		out
//				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//		out.println("<HTML>");
//		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//		out.println("  <BODY>");
//		out.print("    This is ");
//		out.print(this.getClass());
//		out.println(", using the GET method");
//		out.println("  </BODY>");
//		out.println("</HTML>");
//		out.flush();
//		out.close();
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	
	
	
	public void init() throws ServletException {
		// Put your code here
	}

}
